﻿using System;
using System.Collections.Generic;
using System.Linq;
using GraphProcessor;
using IQIGame.Onigao.Logic;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay.Editors
{
    [System.Serializable, NodeMenuItem(LevelEditorConst.Execution + "/" + LevelEditorConst.SwitchElevatorFloor, typeof(LevelServiceGraph))]
    public class LevelServiceExecutionNode_SwitchElevatorFloor : LevelServiceExecutionNode
    {
        protected override ServiceExecutionType mServiceType => ServiceExecutionType.SwitchElevatorFloor;

        public override string name => LevelEditorConst.SwitchElevatorFloor;
        [Output("目标", false)] public EditorLevelServiceTargetSetter tagOrMcid;
        [InspectorName("楼层激活情况")] public List<FloorState> floors;

        protected override LevelServiceCustomConfig CreateConfig()
        {
            var rConfig = new LevelServiceExecutionCustom_SwitchElevatorFloor()
            {
                // tagOrMcid = this.tagOrMcid,
                floors = this.floors.Select((x) => new LevelServiceCustom_FloorState()
                {
                    floor = x.floor,
                    state = x.state
                }).ToList()
            };
            this.tagOrMcid = new EditorLevelServiceTargetSetter(x => rConfig.tagOrMcid = x);
            return rConfig;
        }

        protected override void OnSync(EditorLevelServiceExecutionConfig newConfig, LevelServiceExecutionNode tempNode)
        {
            if (tempNode is LevelServiceExecutionNode_SwitchElevatorFloor rNode)
            {
                this.floors = rNode.floors;
            }
            if (newConfig.customConfig is LevelServiceExecutionCustom_SwitchElevatorFloor rConfig)
            {
                this.SyncTarget(rConfig.tagOrMcid);
            }
        }
        
        [Serializable]
        public class FloorState
        {
            [LevelServiceEditorCustomProperty(ServiceExecutionType.SwitchElevatorFloor, "指定楼层")]
            public int floor;
            [LevelServiceEditorCustomProperty(ServiceExecutionType.SwitchElevatorFloor, "激活状态")]
            public int state;
        }
    }
}